﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;
using System.Data;

namespace WXYW.BLL
{
    public class SJTYService : RepositoryBase
    {
        public List<ApplicationProduct> GetAllProducts()
        {
            return GetEntities<ApplicationProduct>().ToList();
        }

        public void AddProductInfo(DataSet ds)
        {
            try
            {
                string serialNO = Convert.ToString(ds.Tables[0].Rows[0]["Serial"]);
                string pModel = Convert.ToString(ds.Tables[0].Rows[0]["pModel"]);
                DateTime SellTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["SellTime"]);
                string zzUser = Convert.ToString(ds.Tables[0].Rows[0]["zzUser"]);
                string jxsName = Convert.ToString(ds.Tables[0].Rows[0]["jxsName"]);
                string tsfw = Convert.ToString(ds.Tables[0].Rows[0]["tsfw"]);
                string EndTime = Convert.ToString(ds.Tables[0].Rows[0]["EndTime"]);

                ProductInfo productInfo = GetEntities<ProductInfo>().Where(p => p.SerialNO.Equals(serialNO)).FirstOrDefault();
                if (productInfo != null)
                {
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        productInfo.EndTime = Convert.ToDateTime(EndTime);
                    }

                    productInfo.JXSName = jxsName;
                    productInfo.ModelNO = pModel;
                    productInfo.SellTime = SellTime;
                    switch (tsfw)
                    {
                        case "Y":
                            productInfo.TSFW = true;
                            break;
                        case "N":
                            productInfo.TSFW = false;
                            break;
                    }
                    productInfo.ZZUser = zzUser;
                    UpdateEntity(productInfo);
                }
                else
                {
                    productInfo = new ProductInfo();
                    productInfo.SerialNO = serialNO;
                    if (!string.IsNullOrEmpty(EndTime))
                    {
                        productInfo.EndTime = Convert.ToDateTime(EndTime);
                    }

                    productInfo.JXSName = jxsName;
                    productInfo.ModelNO = pModel;
                    productInfo.SellTime = SellTime;
                    switch (tsfw)
                    {
                        case "Y":
                            productInfo.TSFW = true;
                            break;
                        case "N":
                            productInfo.TSFW = false;
                            break;
                    }
                    productInfo.ZZUser = zzUser;
                    InsertEntity(productInfo);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("AddProductInfo - {0}", Convert.ToString(ds.Tables[0].Rows[0]["Serial"])));
            }
        }

        public List<ApplicationForm> GetApplicationsToCRM()
        {
            return GetEntities<ApplicationForm>().Where(a => a.IsSubmit.HasValue && a.IsSubmit.Value).ToList();
        }

        public List<ApplicationProduct> GetApplicationProductToCRM()
        {
            return GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.IsSubmit.HasValue && p.ApplicationForm.IsSubmit.Value
                && !(p.ToCRM.HasValue && p.ToCRM.Value) &&
                ((p.SendWay.Equals("A") && p.NoticeClientDateTime.HasValue) ||
                 p.SendWay.Equals("B") && p.SendBackTime.HasValue) 
                // && p.ClientReceiveDateTime.HasValue
                 ).ToList();
        }

        public List<ApplicationProduct> GetApplicationProductToHF()
        {
            return GetEntities<ApplicationProduct>().Where(p => p.ToCRM.HasValue && p.ToCRM.Value).ToList();
        }

        public void AddTransferLog(SJTYLog logEntity)
        {
            try
            {
                InsertEntity(logEntity);

                if (logEntity.Status.Equals("OK"))
                {
                    ApplicationProduct product = GetEntities<ApplicationProduct>().Where(p => p.ApplicationProductID == logEntity.ApplicationProductID).FirstOrDefault();
                    product.ToCRM = true;
                    product.WXNO = logEntity.WXNO;
                    UpdateEntity(product);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("AddTransferLog::{0}",logEntity.ApplicationProductID));
                throw ex;
            }
        }

        public CustomerFeedback GetCustomerFeedback(string wxno)
        {
            return GetEntities<CustomerFeedback>().Where(c => c.WXNO.Equals(wxno)).FirstOrDefault();
        }

        public void InserCustomerFeedback(CustomerFeedback customerFeedback)
        {
            InsertEntity(customerFeedback);
        }

        public void UpdateCustomerFeedback(CustomerFeedback customerFeedback)
        {
            UpdateEntity(customerFeedback);
        }
    }
}
